<template>
    <div class="PuplicDialog" v-show="show || animateIng" @click="closeDialogOther">
        <transition name="fade">
            <div v-if="showFixedBg" class="fixedBg" @click="closeDialog"></div>
        </transition>
        <transition :name="transitionName" :after-leave="animateEndAfter">
            <div class="topDialog" :style="topStyle" v-if="props.position == 'top'">
                <div :class="props.type" v-if="props.type == 'default' || !props.type">
                    <div class="content" v-if="props.content.info">{{props.content.info}}</div>
                    <div class="closeBtn" @click="closeDialog">
                        <div class="closeIco"></div>
                    </div>
                </div>
                <div :class="props.type" v-else-if="props.type == 'successful'">
                    <div class="content" v-if="props.content.info">{{props.content.info}}</div>
                    <div class="closeBtn" @click="closeDialog">
                        <div class="closeIco"></div>
                    </div>
                </div>
                <div :class="props.type" v-else-if="props.type == 'error'">
                    <div class="content" v-if="props.content.info">{{props.content.info}}</div>
                    <div class="closeBtn" @click="closeDialog">
                        <div class="closeIco"></div>
                    </div>
                </div>
            </div>
            <div class="middleDialog" v-else-if="props.position == 'middle'">
                <div class="popup">
                    <div v-if="props.type == 'default' || !props.type">
                        <div class="title" v-if="props.content.title">{{props.content.title}}</div>
                        <div class="content" v-if="props.content.info">
                            {{props.content.info}}
                        </div>
                        <ul class="btnArea main_color" v-if="props.content.btn && props.content.btn.length > 0">
                            <li v-for="item in props.content.btn" @click="runCallback(item.btnCallback)">{{item.label}}</li>
                        </ul>
                    </div>
                    <div v-else-if="props.type == 'confirm'">
                        <div class="title" v-if="props.content.title">{{props.content.title}}</div>
                        <div class="content" v-if="props.content.info">
                            {{props.content.info}}
                        </div>
                        <ul class="btnArea main_color" v-if="props.type == 'confirm'">
                            <li @click="closeDialog">{{language.unify_cancel}}</li>
                            <li @click="runCallback(props.callback)">{{language.unify_ok}}</li>
                        </ul>
                    </div>
                    <div v-else-if="props.type == 'upgrade'">
                        <div class="upgradeIco"></div>
                        <div class="upgradeText" v-if="props.content.info">{{props.content.info}}</div>
                        <div class="btn btnUpgrade" @click="runCallback(props.callback)">Learn more</div>
                    </div>
                </div>
            </div>
            <div class="bottomDialog" v-else-if="props.position == 'bottom'">
                <div class="wrapper" v-if="props.type == 'default' || !props.type">
                    <ul v-if="props.content.btn && props.content.btn.length > 0">
                        <li v-for="item in props.content.btn" @click="runCallback(item.btnCallback)">{{item.label}}</li>
                    </ul>
                    <ul>
                        <li @click="closeDialog">{{language.unify_cancel}}</li>
                    </ul>
                </div>
            </div>
        </transition>
    </div>
</template>
<script>
export default {

    /*props: {
        position: 'middle', //Display position
        top: '0', //Distance at the top of the distance
        type: 'default', //type
        content: {
            title: '', //The title
            info: '', //The content
            /*btn: [{
                label: '', //Button text
                btnCallback: function() {} // Button Callback
            }]*/ //Custom button
    //},
    //callback: false // Callback function
    //}

    name: 'puplicDialog',

    props: ['props'],

    data: function() {
        return {
            show: false,
            topStyle: '',
            transitionName:'upDown',
            animateIng:false,
        }
    },

    computed: {

        showFixedBg:function(){
            if(this.props.position && this.props.position != 'top'){
                return true;
            }
        },

        // dialogClass : function(){
        //     var dialogClass;
        //     if(this.props.position == 'top'){
        //         dialogClass = 'topDialog'
        //     }else if(this.props.position == 'middle'){
        //         dialogClass = 'middleDialog'
        //     }else if(this.props.position == 'bottom'){
        //         dialogClass = 'bottomDialog'
        //     }
        //     return dialogClass;
        // }
    },

    watch: {
        'props.position': function(val) {
            let tthis = this;
            this.show = true;
            if (val == 'top') {
                tthis.transitionName = 'fade';
                if (typeof(tthis.props.top) != 'undefined') {
                    tthis.topStyle = 'top:' + tthis.props.top + 'px';
                }
                setTimeout(function() {
                    if (tthis.props.position == 'top'){
                        tthis.closeDialog();
                    }
                }, 5000);
            }else{
                tthis.transitionName = 'upDown';
            }
        },
    },

    methods: {

        animateEndAfter : function(){
            this.animateIng = false;
        },

        closeDialog: function() {
            this.animateIng = true;
            this.props.position ='';
            this.show = false;
        },

        runCallback: function(callback) {
            this.closeDialog();
            if (callback) {
                callback();
            } else if (this.props.type == 'upgrade' && !callback) {
                this.$router.push('/upgrade')
            }
        },

        closeDialogOther: function(e) {
            if (e.srcElement.parentNode.className == 'PuplicDialog') {
                this.closeDialog();
            }
        },
    }
}

</script>
<style scoped>
/*common*/
.fixedBg{
    position: fixed;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 9999;
    background-color: rgba(0, 0, 0, .5);
    animation: bgFadeIn .2s linear;
}
/*end common*/

.middleDialog {
    position: fixed;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.middleDialog .popup {
    background-color: #fff;
    border-radius: 5px;
    margin: auto;
    width: 100%;
    max-width: 300px;
    font-size: 12px;
    position: relative;
    padding: 10px 0 0 0;
    color: #333;
    overflow: hidden;
}


.middleDialog .title {
    min-height: 30px;
    padding: 5px 15px;
    line-height: 20px;
    box-sizing: border-box;
    font-size: 16px;
    text-align: center;
    font-weight: bold;
    color: #000;
}

.middleDialog .content {
    font-size: 14px;
    padding: 0 15px;
    text-align: center;
    margin: 10px 0 20px 0;
}

.middleDialog .btnArea {
    display: flex;
    height: 40px;
    border-top: 1px solid #ccc;
}

.middleDialog .btnArea li {
    float: 1;
    -ms-flex: 1;
    flex: 1;
    border-right: 1px solid #ccc;
    line-height: 40px;
    text-align: center;
    overflow: hidden;
    font-weight: 600;
    font-size: 14px;
    /*text-transform: capitalize;*/
}

.middleDialog .btnArea li:last-child {
    border: 0;
}

.middleDialog .upgradeIco {
    margin: auto;
    width: 90px;
    height: 90px;
    background: url(../assets/ico_upgrade_gold.png) center no-repeat;
    background-size: 50px auto;
}

.middleDialog .upgradeText {
    padding: 10px 15px 40px 15px;
    font-size: 16px;
    line-height: 20px;
    text-align: center;
    font-weight: 600;
}

.middleDialog .btnUpgrade {
    border-radius: 0;
    height: 48px;
    line-height: 48px;
    font-size: 16px;
}

.topDialog {
    position: absolute;
    top: 44px;
    width: 100%;
    min-height: 50px;
    z-index: 9999;
}

.topDialog .default,
.topDialog .successful,
.topDialog .error {
    position: relative;
    min-height: 50px;
    background-color: #ff634a;
    box-sizing: border-box;
    padding: 15px 40px 15px 15px;
    color: #fff;
    line-height: 20px;
    font-size: 12px;
}

.topDialog .successful {
    background-color: #33d323;
}

.topDialog a {
    text-decoration: underline;
}

.topDialog a:active,
.topDialog a:hover {
    text-decoration: none;
}

.topDialog .closeBtn {
    position: absolute;
    height: 50px;
    right: 0;
    top: 0;
    width: 40px;
    text-align: center;
    line-height: 50px;
    white-space: nowrap;
}

.closeIco {
    position: relative;
    display: inline-block;
    width: 12px;
    height: 12px;
    overflow: hidden;
}

.closeIco:before,
.closeIco:after {
    content: '';
    position: absolute;
    height: 2px;
    width: 100%;
    top: 50%;
    left: 0;
    margin-top: -1px;
    background: #000;
}

.topDialog .closeBtn .closeIco:before,
.topDialog .closeBtn .closeIco:after {
    background: #fff;
}

.closeIco:before {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
}

.closeIco:after {
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
}

.bottomDialog{
    position: fixed;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 9999;
}

.bottomDialog .wrapper {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    padding: 0 14px;
    box-sizing: border-box;
}

.bottomDialog .wrapper ul {
    width: 100%;
    font-size: 14px;
    text-align: center;
    margin-bottom: 14px;
    overflow: hidden;
    border-radius: 5px;
    color: #333;
}

.bottomDialog .wrapper li {
    width: 100%;
    line-height: 46px;
    background-color: #fff;
    border-bottom: 1px solid #ccc;
}

.bottomDialog .wrapper li:last-child {
    border-bottom: 0;
}


/*animate*/
.fade-enter-active,
.fade-leave-active {
    transition: opacity .4s
}
.fade-leave-active {
    opacity: 0
}

.upDown-enter-active {
    animation: fadeInUp .4s cubic-bezier(0.19, 1, 0.22, 1);
    -webkit-animation-name: fadeInUp;
    animation-name: fadeInUp
}

.upDown-leave-active {
    animation: fadeOutDown .4s cubic-bezier(0.19, 1, 0.22, 1);
    -webkit-animation-name: fadeOutDown;
    animation-name: fadeOutDown
}

@-webkit-keyframes bgFadeIn {
    0% {
        background-color: rgba(0, 0, 0, .0);
    }
    100% {
        background-color: rgba(0, 0, 0, .5);
    }
}

@keyframes bgFadeIn {
    0% {
        background-color: rgba(0, 0, 0, .0);
    }
    100% {
        background-color: rgba(0, 0, 0, .5);
    }
}

@-webkit-keyframes fadeInUp {
    0% {
        opacity: 0;
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0)
    }
    100% {
        opacity: 1;
        -webkit-transform: none;
        transform: none
    }
}

@keyframes fadeInUp {
    0% {
        opacity: 0;
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0)
    }
    100% {
        opacity: 1;
        -webkit-transform: none;
        transform: none
    }
}

@-webkit-keyframes fadeOutDown {
    0% {
        opacity: 1
    }
    100% {
        opacity: 0;
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0)
    }
}

@keyframes fadeOutDown {
    0% {
        opacity: 1
    }
    100% {
        opacity: 0;
        -webkit-transform: translate3d(0, 100%, 0);
        transform: translate3d(0, 100%, 0)
    }
}


/*end animate*/
</style>